package vip.denglei.company.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import vip.denglei.company.model.app.AppUserAddress;
import vip.denglei.company.service.app.AppUserAddressService;
import vip.denglei.company.util.HttpResult;
import vip.denglei.company.util.kuidi100.KdUtil;

import javax.annotation.Resource;

@RequestMapping(value = "/appUserAddress")
@RestController
@Slf4j
public class AppUserAddressController {
    @Resource
    private AppUserAddressService appUserAddressService;

    /**
     * 保存用户收货地址
     *
     * @param appUserAddress
     * @return
     */
    @PostMapping("save/appUserAddress")
    public HttpResult saveAppUserAddress(@RequestBody AppUserAddress appUserAddress) {
        log.info("AppUserAddressController saveAppUserAddress 参数 appUserAddress:{}", appUserAddress);
        return appUserAddressService.saveAppUserAddress(appUserAddress);

    }

    /**
     * 修改用户收货地址
     *
     * @param appUserAddress
     * @return
     */
    @PutMapping("update/appUserAddress")
    public HttpResult updateAppUserAddress(@RequestBody AppUserAddress appUserAddress) {
        log.info("AppUserAddressController updateAppUserAddress 参数 appUserAddress:{}", appUserAddress);
        return appUserAddressService.saveAppUserAddress(appUserAddress);
    }

    /**
     * 删除收货地址
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete/appUserAddress/{id}")
    public HttpResult deleteAppUserAddress(@PathVariable Integer id) {
        log.info("AppUserAddressController deleteAppUserAddress 参数 id:{}", id);
        LambdaUpdateWrapper<AppUserAddress> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(AppUserAddress::getId, id);
        lambdaUpdateWrapper.set(AppUserAddress::getDeleteFlag, 1);
        appUserAddressService.update(lambdaUpdateWrapper);
        return HttpResult.success();
    }

    @GetMapping("query/appUserAddressPage/{userId}/{pageNum}/{pageSize}")
    public HttpResult queryAppUserAddressPageByUserId(@PathVariable String userId, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        log.info("AppUserAddressController queryAppUserAddresPageByUserId 参数 userId:{},pageNum:{},pageSize:{}", userId, pageNum, pageNum);
        Page<AppUserAddress> page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<AppUserAddress> userAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userAddressLambdaQueryWrapper.eq(AppUserAddress::getUserId, userId);
        userAddressLambdaQueryWrapper.eq(AppUserAddress::getDeleteFlag, 0);

        return HttpResult.success(appUserAddressService.page(page, userAddressLambdaQueryWrapper));
    }

    @GetMapping("query/appUserAddressPage/addressRecognition")
    public HttpResult queryAppUserAddressPageAddressRecognition(@RequestParam String addressStr) {
        log.info("AppUserAddressController queryAppUserAddresPageAddressRecognition 参数 addressStr:{}}", addressStr);
        try {
            return HttpResult.success(KdUtil.queryAddressRecognition(addressStr  ));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


}
